package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo14 {
    public static void main(String[] args) {
        Callable<Integer> callable = new Callable<Integer>() {  //这是描述一个任务
            @Override
            public Integer call() throws Exception {
                int num = 0;
                for (int i = 1; i <= 1000 ; i++) {
                    num += i;
                }
                return num;
            }
        };
        FutureTask<Integer> task = new FutureTask<>(callable);  //  与每个任务对应，好似点餐时的单号，对应
        Thread t = new Thread(task);   //其中一个构造方法可以参数为Future,提交执行
        t.start();
        //不可能拿着单号就能端菜，毕竟菜可能还没做好，这时会进入阻塞
        try {
            System.out.println(task.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
